
///<reference path="../../../../js_plugins/jsmind/jsmind.d.ts"/>
import { AlertController } from 'ionic-angular';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

/**
 * Generated class for the JsmindPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-jsmind',
  templateUrl: 'jsmind.html',
})
export class JsmindPage {
  js_mind: any;
  constructor(public navCtrl: NavController, public navParams: NavParams, private alCtrl: AlertController) {

  }



  ionViewDidLoad() {
    console.log('ionViewDidLoad JsmindPage');
    this.init_mind();
  }
  init_mind() {

    let mind = {
      meta: {
        name: 'demo',
        author: 'hizzgdev@163.com',
        version: '0.2'
      },
      format: 'node_array',
      data: [
        { "id": "root", "isroot": true, "topic": "jsMind" },

        { "id": "sub1", "parentid": "root", "topic": "sub1" },
        { "id": "sub11", "parentid": "sub1", "topic": "sub11" },
        { "id": "sub12", "parentid": "sub1", "topic": "sub12" },
        { "id": "sub13", "parentid": "sub1", "topic": "sub13" },

        { "id": "sub2", "parentid": "root", "topic": "sub2" },
        { "id": "sub21", "parentid": "sub2", "topic": "sub21" },
        { "id": "sub22", "parentid": "sub2", "topic": "sub22" },

        { "id": "sub3", "parentid": "root", "topic": "sub3" },
      ]
    };
    let options = {
      container: 'js_mind',
      editable: true,
      theme: 'undline'
    }
    this.js_mind = jsMind.show(options, mind);
  }

  add_child() {
    let selednod = this.js_mind.get_selected_node();
    if (!!selednod) {
      let nodeid = jsMind.util.uuid.newid();
      let node = this.js_mind.add_node(selednod, nodeid, '新节点');
      if (!!node) {
        this.js_mind.select_node(nodeid);
        // this.js_mind.begin_edit(nodeid);
        this.openAl(nodeid, node.topic);
      }
    }
  }

  remove_child() {
    let selednod = this.js_mind.get_selected_node();
    if (!!selednod) {
      this.js_mind.remove_node(selednod);
    }
  }
  eidt_child() {
    let selednod = this.js_mind.get_selected_node();
    if (!!selednod) {
      // this.js_mind.begin_edit(selected_node)
      this.openAl(selednod.id, selednod.topic);
    }
  }

  openAl(selid, topic) {
    let al = this.alCtrl.create({
      message: '请输入节点内容',
      inputs: [{
        name: 'topic',
        placeholder: "节点内容",
        value: topic,
        type: "text"
      }],
      buttons: [{
        text: "取消",
        role: "cancle"
      }, {
        text: "确认",
        handler: (data) => {
          // console.log(data.topic);
          // console.log(seled);
          this.js_mind.update_node(selid, data.topic)
        }
      }]
    });
    al.present();
  }

}
